<?php

namespace App\Http\Controllers\Service;

use App\Exceptions\GeneralException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Station\Home\HomeCreate;
use App\Http\Requests\Station\Home\HomeDelivery;
use App\Http\Requests\Station\Home\HomeExpress;
use App\Http\Requests\Station\Home\WorkRetreat;
use App\Repositories\OrderRepository;
use App\Repositories\UserLogRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Repositories\HomeRepository;
use App\Http\Requests\Station\Home\ShowRequest;
use App\Http\Requests\Station\Home\QueryResource;

class HomeController extends Controller
{
    protected $homeRepository;
    protected $userLogRepository;
    protected $orderRepository;

    /**
     * HomesController constructor.
     * @param HomeRepository $homeRepository
     * @param UserLogRepository $userLogRepository
     * @param OrderRepository $orderRepository
     */
    public function __construct(
        HomeRepository $homeRepository,
        UserLogRepository $userLogRepository,
        OrderRepository $orderRepository
    )
    {
        $this->homeRepository = $homeRepository;
        $this->userLogRepository = $userLogRepository;
        $this->orderRepository = $orderRepository;
    }

    /**
     * 获取首页列表资源列表
     *
     * Date: 2018/06/02
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryResource $request)
    {
        $admins = Auth::guard('station')->user();
        $service_id = $admins->service_id;
        $homes = $this->homeRepository->getHomes($service_id);
        return success($homes);
    }

    /**
     * 入库
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param HomeCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function entry(HomeCreate $request)
    {
        $works = $this->orderRepository->AdminAddOrder($request);
        return success($works);
    }

    /**
     * 出库
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param HomeDelivery $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function delivery(HomeDelivery $request)
    {
        $works = $this->orderRepository->adminDeliveryOrders($request);
        return success($works);
    }

    /**
     * 出库数据
     *
     * Date: 2018/07/03
     * @author Tenny
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deliveryData(Request $request)
    {
        $limit = $request->get('paginate', false);
        if ($limit == null) {
            $limit = false;
        }
        $works = $this->orderRepository->deliveryData($limit);
        return success($works);
    }

    /**
     * 出库数据
     *
     * Date: 2018/07/03
     * @author Tenny
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deliveryCount(Request $request)
    {
        $works = [
            'output' => $this->orderRepository->deliveryData(false, true),
        ];
        return success($works);
    }

    /**
     * 退回网点
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param WorkRetreat $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function retreat(WorkRetreat $request)
    {
        $works = $this->orderRepository->retreat($request);
        return success($works);
    }

    /**
     * 退回网点数据
     *
     * Date: 2018/07/03
     * @author Tenny
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function retreatData(Request $request)
    {
        $limit = $request->get('paginate', false);
        if ($limit == null) {
            $limit = false;
        }
        $works = $this->orderRepository->retreatData($limit);
        return success($works);
    }

    /**
     * 出库数据
     *
     * Date: 2018/07/03
     * @author Tenny
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function retreatCount(Request $request)
    {
        $works = [
            'output' => $this->orderRepository->retreatData(false, true),
        ];
        return success($works);
    }

    /**
     * 获取物流公司
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param HomeExpress $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function express(HomeExpress $request)
    {
        $user = Auth::guard('station')->user();
        $service_id = $user->service_id;
        $result = [];
        $order_id = $request->get('single_number', false);
        $res = $this->orderRepository->matchingNetwork($service_id, $order_id);
        if (is_array($res) && count($res) > 0) {
            $result['name'] = $res['code_name'];
            $result['come'] = $res['express_no'];
            $result['network_name'] = $res['network_name'];
            $result['take_code'] = $this->orderRepository->getTakeCode($service_id);
            unset($res);
            return success($result);
        } else {
            return failed('查询失败');
        }
    }

    /**
     * 获取取件码
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function takeCode(Request $request)
    {
        $user = Auth::guard('station')->user();
        $service_id = $user->service_id;
        $code = $this->orderRepository->getTakeCode($service_id);
        return success(['code' => $code]);
    }
}
